package arithmetic;

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author jiangfeng
 * Created on 2020-03-31
 */
public class Test {


    public static void main(String[] args) {
        // testPowerOfTwo();
        //testchoose();
        testRunnable();
        testRunnable();
    }

    // 乘方；幂
    static void testPowerOfTwo(){
        System.out.println(isPowerOfTwo(-1));
        System.out.println(isPowerOfTwo(-2));
        System.out.println(isPowerOfTwo(0));
        System.out.println(isPowerOfTwo(1));
        System.out.println(isPowerOfTwo(2));
        System.out.println(isPowerOfTwo(3));
        System.out.println(isPowerOfTwo(4));
        System.out.println(isPowerOfTwo(5));
        System.out.println(isPowerOfTwo(6));
        System.out.println(isPowerOfTwo(7));
        System.out.println(isPowerOfTwo(8));
        System.out.println(isPowerOfTwo(9));
        System.out.println(isPowerOfTwo(10));
        System.out.println(isPowerOfTwo(11));
        System.out.println(isPowerOfTwo(12));
        System.out.println(isPowerOfTwo(13));
        System.out.println(isPowerOfTwo(14));
        System.out.println(isPowerOfTwo(15));
        System.out.println(isPowerOfTwo(16));
        System.out.println(isPowerOfTwo(20));
        System.out.println(isPowerOfTwo(32));
    }

    // 是否是2的n次幂
    static boolean isPowerOfTwo(int val) {

        // 计算2的n次幂
        System.out.println(val+" "+(val & -val));

        return (val & -val) == val;
    }


    static AtomicInteger idx = new AtomicInteger();


    static void choose(int length){
        for (int i = 0; i < 20; i++) {
            System.out.print(idx.getAndIncrement() & length - 1);
            System.out.print(" ");
        }
        System.out.println();

    }

    public static void testchoose(){
        for (int i = 1; i <= 16; i++) {
            System.out.println("------"+i);
            choose(i);
        }

    }

    /**
     * 保证校验失败后程序仍然进行
     */
    private static void excute(Runnable task) {
        try {
            task.run();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 测试runnable
     */
    static void testRunnable() {
        excute(() -> {
            System.out.println("啦啦");
            int i = 1 / 0;
        });
    }

    @org.junit.jupiter.api.Test
    public void test33(){
        ZoneId zone1 = ZoneId.of("Europe/Berlin");
        ZoneId zone2 = ZoneId.of("Brazil/East");
        LocalTime now1 = LocalTime.now(zone1);
        LocalTime now2 = LocalTime.now(zone2);
        System.out.println(now1.isBefore(now2));  // false
        long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
        long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
        System.out.println(hoursBetween);       // -3
        System.out.println(minutesBetween);     // -239
    }

}
